/*
    Pop Confirm
    author: zhao xin
    date: 2017.4
*/


! function($) {
	'use strict';

	$.fn.tooltip = function () {
		this.each(function(index, el) {
			var $trigger = $(this);

			// do nothing if tooltip is already initialized.
			if ($trigger.data('tooltip-init')) {
				return;
			}

			var $tooltip;

			$trigger.hover(function() {
				$tooltip = $('<div class="mui-pop mui-tooltip"></div>');

				var content = $trigger.data('tooltip');
				var $arrow = $('<div class="mui-pop-arrow"></div>');

				$tooltip.text(content);
				$arrow.appendTo($tooltip);
				$tooltip.appendTo('body');

				var	position = $trigger.data('position') || 'top',
					triggerTop = $trigger.offset().top,
					triggerLeft = $trigger.offset().left,
					triggerWidth = $trigger.outerWidth(),
					triggerHeight = $trigger.outerHeight(),
					popWidth = $tooltip.outerWidth(),
					popHeight = $tooltip.outerHeight(),
					top, left;

				switch (position) {
					case 'top':
						top = triggerTop - popHeight - 8;
						left = triggerLeft - popWidth / 2 + triggerWidth / 2;
						$arrow.css({
							'left': '50%',
							'top': '100%',
							'margin-top': '-4px',
							'margin-left': '-3px',
							'border-top': '0',
							'border-left': '0'
						});
						break;
					case 'bottom':
						top = triggerTop + triggerHeight + 8;
						left = triggerLeft - popWidth / 2 + triggerWidth / 2;
						$arrow.css({
							'left': '50%',
							'top': '0',
							'margin-top': '-2px',
							'margin-left': '-3px',
							'border-bottom': '0',
							'border-right': '0'
						});
						break;
					case 'left':
						top = triggerTop - popHeight / 2 + triggerHeight / 2;
						left = triggerLeft - popWidth - 8;
						$arrow.css({
							'left': '100%',
							'top': '50%',
							'margin-top': '-3px',
							'margin-left': '-4px',
							'border-bottom': '0',
							'border-left': '0'
						});
						break;
					case 'right':
						top = triggerTop - popHeight / 2 + triggerHeight / 2;
						left = triggerLeft + triggerWidth + 8;
						$arrow.css({
							'left': '0',
							'top': '50%',
							'margin-top': '-3px',
							'margin-left': '-2px',
							'border-top': '0',
							'border-right': '0'
						});
						break;
					default:
				}
				$tooltip.css({
					'top': top,
					'left': left
				});
			}, function() {
				$tooltip.remove();
			});
		});
	}

	$(document).ready(function($) {
		$('[data-tooltip]').tooltip();
	});
}(jQuery);